package ge.telasi.tasks.ui.comp;

import ge.telasi.tasks.ui.UIUtils;
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class CalendarField extends JPanel {

    /** Creates new form FDateField */
    public CalendarField() {
        initComponents();
        setDate(null);
        Dimension size = btnClear.getPreferredSize();
        btnClear.setPreferredSize(new Dimension(25, size.height));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtDate = new javax.swing.JTextField();
        btnClear = new javax.swing.JButton();

        setOpaque(false);
        setLayout(new java.awt.BorderLayout());

        txtDate.setEditable(false);
        txtDate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDateMouseClicked(evt);
            }
        });
        add(txtDate, java.awt.BorderLayout.CENTER);

        btnClear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/delete.png"))); // NOI18N
        btnClear.setToolTipText("გასუფთავება");
        btnClear.setBorderPainted(false);
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });
        add(btnClear, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents

    private void txtDateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDateMouseClicked
        change();
}//GEN-LAST:event_txtDateMouseClicked

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        setDate(null);
    }//GEN-LAST:event_btnClearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JButton btnClear;
    protected javax.swing.JTextField txtDate;
    // End of variables declaration//GEN-END:variables

    private Calendar date;

    public Calendar getDate() {
        return date;
    }

    public void setDate(Calendar date) {
        this.date = date;
        reset();
    }

    void reset() {
        if (date == null) {
            txtDate.setText("(განუსაზღვრელი)");
        } else {
            SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
            txtDate.setText(f.format(date.getTime()));
        }
        txtDate.setCaretPosition(0);
    }

    private void change() {
        CalendarDialog dialog = new CalendarDialog(this);
        UIUtils.placeNearField(dialog, txtDate);
        dialog.setDate(date);
        dialog.setVisible(true);
        if (dialog.isApproved()) {
            setDate(dialog.getDate());
        }
    }

}
